package com.jiutong.meipenggang.ui.activitys;


import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.jiutong.meipenggang.R;
import com.jiutong.meipenggang.base.AppManager;
import com.jiutong.meipenggang.base.BaseActivity;
import com.jiutong.meipenggang.common.Constants;
import com.jiutong.meipenggang.utils.NumUilt;
import com.jiutong.meipenggang.utils.SpConfig;
import com.jiutong.meipenggang.views.dialog.BaseDialog;
import com.orhanobut.logger.Logger;

import butterknife.Bind;
import butterknife.OnClick;

public class SettingActivity extends BaseActivity {


    @Bind(R.id.rl_common_title_back)
    RelativeLayout rlCommonTitleBack;
    @Bind(R.id.tv_common_title)
    TextView tvCommonTitle;
    @Bind(R.id.tv_common_title_options)
    TextView tvCommonTitleOptions;
//    @Bind(R.id.tv_setting_change_phone)
//    TextView tvSettingChangePhone;
    @Bind(R.id.tv_setting_phone)
    TextView tvSettingPhone;
    @Bind(R.id.rl_setting_change_phone)
    RelativeLayout rlSettingChangePhone;
    @Bind(R.id.tv_setting_change_pwd)
    TextView tvSettingChangePwd;
    @Bind(R.id.rl_setting_change_pwd)
    RelativeLayout rlSettingChangePwd;
    @Bind(R.id.rl_setting_notify)
    RelativeLayout rlSettingNotify;
    @Bind(R.id.toggle_more)
    ToggleButton toggleMore;
    @Bind(R.id.rl_setting_change_shoushi)
    RelativeLayout rlSettingChangeShoushi;
    @Bind(R.id.btn_setting_exit)
    Button btnSettingExit;

    private BaseDialog dialog;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView() {
        super.initView();
        tvCommonTitle.setText("设置");

        //重置手势密码
//        resetGesture();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (TextUtils.isEmpty(SpConfig.getInstance().getString(Constants.SHOU_SHI))) {
            toggleMore.setChecked(false);
            rlSettingChangeShoushi.setVisibility(View.GONE);
        } else {
            toggleMore.setChecked(true);
            rlSettingChangeShoushi.setVisibility(View.VISIBLE);
        }
        //获取当前设置手势密码的ToggleButton的状态
        getGestureStatus();

        String phone = SpConfig.getInstance().getString(Constants.USERNAME_STRING);
        if(TextUtils.isEmpty(phone))
            return;
        tvSettingPhone.setText(NumUilt.phoneFormat(phone));

        if (TextUtils.isEmpty(SpConfig.getInstance().getString(Constants.SHOU_SHI))) {
            toggleMore.setChecked(false);
            rlSettingChangeShoushi.setVisibility(View.GONE);
        } else {
            toggleMore.setChecked(true);
            rlSettingChangeShoushi.setVisibility(View.VISIBLE);
        }
        //获取当前设置手势密码的ToggleButton的状态
        getGestureStatus();

//        //设置手势密码
//        setGesturePassword();

    }

    @OnClick({R.id.rl_common_title_back, R.id.rl_setting_change_pwd, R.id.rl_setting_notify,R.id.toggle_more,R.id.rl_setting_change_shoushi, R.id.btn_setting_exit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_common_title_back:
                finish();
                break;
//            case R.id.rl_setting_change_phone:
                //暂不可以修改手机号码
//                openActivity(ChangePhoneActivity.class);
//                break;
            case R.id.rl_setting_change_pwd:
                openActivity(ChangePwdActivity.class);
                break;
            case R.id.rl_setting_notify:
                openActivity(MsgActivity.class);
                break;
            case R.id.toggle_more:
                boolean checked = toggleMore.isChecked();
                if (checked) {
                    openActivity(GestureEditActivity.class);
                } else {
                    toastShortShow("手势密码操作已关闭，请开启后再设置");
                    rlSettingChangeShoushi.setVisibility(View.GONE);
                    SpConfig.getInstance().edit().putString(Constants.SHOU_SHI,"").commit();
                }
                break;
            case R.id.rl_setting_change_shoushi:
                openActivity(GestureVerifyActivity.class);
                SpConfig.getInstance().edit().putBoolean(Constants.CHANGE_SHOUSHI,true).commit();
                break;
            case R.id.btn_setting_exit:
                if (dialog == null) {
                    dialog = new BaseDialog.Builder().
                            layResuorce(R.layout.dialog_common).
                            cancelViewClickEvent(R.id.tv_dialog_cancel).
                            okViewClickEvent(R.id.tv_dialog_ok).
                            confirmId(R.id.tv_dialog_confirm).
                            llBottomId(R.id.ll_dialog).
                            contentTextId(R.id.tv_dialog_content).
                            hintTextId(R.id.tv_dialog_hint).
                            setHintText("温馨提示").
                            setContentText("是否退出当前账户？").
                            setCancelText("否").
                            setOkText("是").
                            setConfirmText("确定").
                            gravityStyle(Gravity.CENTER).
                            animResource(R.style.dialog_animation_fade).
                            dialogWidth(-2).
                            build();
                    dialog.setOnDialogClickListener(new BaseDialog.OnDialogClickListener() {
                        @Override
                        public void onOkClick() {
                            dialog.dismiss();
//                            SpConfig.getInstance().putString(Constants.SESSIONID_STRING, "");
//                            SpConfig.getInstance().putBool(Constants.ISAUTH_BOOLEAN, false);
//                            SpConfig.getInstance().putString(Constants.REALNAME_STRING, "");
                            SpConfig.getInstance().edit().putString(Constants.TEPPHONE_STRING, "").commit();
                            SpConfig.getInstance().edit().putBoolean(Constants.IS_LOGIN,false).commit();
                            SpConfig.getInstance().edit().putBoolean(Constants.CHANGE_SHOUSHI,false).commit();
                            SpConfig.getInstance().edit().putString(Constants.TOKEN,"").commit();
                            SpConfig.getInstance().edit().putString(Constants.ID,"").commit();
                            finish();
                        }

                        @Override
                        public void onOk2Click() {

                        }
                    });
                }

                dialog.show(getSupportFragmentManager(), TAG);
                break;
            default:
        }
    }

    private void getGestureStatus() {
       if(!TextUtils.isEmpty(SpConfig.getInstance().getString(Constants.SHOU_SHI))){
           toggleMore.setChecked(true);
       }else{
           toggleMore.setChecked(false);
       }

    }
}
